package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.ApartmentInfo;
import com.atguigu.lease.model.entity.FacilityInfo;
import com.atguigu.lease.model.entity.GraphInfo;
import com.atguigu.lease.model.entity.LabelInfo;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

  @Autowired
  private LabelInfoMapper labelInfoMapper;
  @Autowired
  private GraphInfoMapper graphInfoMapper;
  @Autowired
  private RoomInfoMapper roomInfoMapper;
  @Autowired
  private FacilityInfoMapper facilityInfoMapper;
    @Override
    public ApartmentItemVo selectApartmentInfoByApartmentId(Long apartmentId) {
        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();

        //公寓的基本信息
        ApartmentInfo apartmentInfo = this.getById(apartmentId);
        List<LabelInfo> labelInfoList=labelInfoMapper.selectLabelByApartmentId(apartmentId);
        List<GraphInfo> graphInfoList=graphInfoMapper.selectGraphByItemIdAndItemType(apartmentId,ItemType.APARTMENT);
        BigDecimal minRent=roomInfoMapper.selectMinRentByApartmentId(apartmentId);

        BeanUtils.copyProperties(apartmentInfo,apartmentItemVo);
        apartmentItemVo.setLabelInfoList(labelInfoList);
        apartmentItemVo.setGraphVoList(graphInfoList);
        apartmentItemVo.setMinRent(minRent);

        return apartmentItemVo;
    }

    @Override
    public ApartmentDetailVo getDetailById(Long apartmentId) {

        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        //公寓的基本信息
        ApartmentInfo apartmentInfo = this.getById(apartmentId);
        //公寓的标签
        List<LabelInfo> labelInfoList=labelInfoMapper.selectLabelByApartmentId(apartmentId);
        //公寓图片
        List<GraphInfo> graphInfoList=graphInfoMapper.selectGraphByItemIdAndItemType(apartmentId,ItemType.APARTMENT);
        //公寓中房间的最小租金
        BigDecimal minRent=roomInfoMapper.selectMinRentByApartmentId(apartmentId);
        //公寓的配套
        List<FacilityInfo> facilityInfoLis= facilityInfoMapper.selectFacilityByApartmentId(apartmentId);

        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setGraphVoList(graphInfoList);
        apartmentDetailVo.setMinRent(minRent);
        apartmentDetailVo.setIsDelete(apartmentInfo.getIsDeleted()==1);
        apartmentDetailVo.setFacilityInfoList(facilityInfoLis);
        return apartmentDetailVo;
    }
}